package com.hhkj.study.admin.user.service.extend.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Maps;
import com.hhkj.study.admin.user.entity.User;
import com.hhkj.study.admin.user.model.podo.UserDo;
import com.hhkj.study.admin.user.model.podo.UserPo;
import com.hhkj.study.admin.user.service.base.UserService;
import com.hhkj.study.admin.user.service.extend.UserExtendService;
import com.ruoyi.common.base.biz.service.extend.BaseExtendServiceImpl;
import com.ruoyi.common.cont.SysConst;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
*
* @author SERVER
* @since 2024-09-06
*/
@Service
public class UserExtendServiceImpl extends BaseExtendServiceImpl<User, UserDo, UserPo, UserService> implements UserExtendService {

    @Override
    public User getByMobileOrIdCard(String mobile, String idCard) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.and(w -> w.eq(StrUtil.isNotBlank(mobile), User::getMobile, mobile).or().eq(StrUtil.isNotBlank(idCard), User::getIdCard, idCard)).last("LIMIT 1");
        return this.getService().getOne(queryWrapper);
    }

    @Override
    public User getByThirdPartyId(String thirdPartyId) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getWxId, thirdPartyId).last("LIMIT 1");
        return this.getService().getOne(queryWrapper);
    }

    @Override
    public Map<Long, User> getUserMapByIdList(Collection<Long> idList) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(User::getId, idList);
        List<User> userList = this.getService().list(queryWrapper);
        Map<Long, User> userMap = Maps.newHashMap();
        if (CollectionUtil.isNotEmpty(userList)) {
           userMap = userList.stream().collect(Collectors.toMap(User::getId, Function.identity()));
        }
        return userMap;
    }

    @Override
    public List<User> selectUserList(Collection<String> mobileList) {
        LambdaQueryWrapper<User> userLambdaQuery = new LambdaQueryWrapper<>();
        userLambdaQuery.in(User::getMobile, mobileList).eq(User::getDisableFlag, SysConst.Flag.NO.getFlag());
        return this.getService().list(userLambdaQuery);
    }
}
